{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fa50af43-7ea0-4112-9d3d-c08144a22acb",
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'clf'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m/tmp/ipykernel_2392805/3141105820.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     11\u001b[0m \u001b[0;31m#将训练好的模型，准确率，PCA导入\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"clf\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'rb'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     13\u001b[0m    \u001b[0mclf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpickle\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;31m#随机森林模型\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     14\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"acc\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'rb'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'clf'"
     ]
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import cv2 as cv\n",
    "import pickle\n",
    "import numpy as np\n",
    "\n",
    "#加载数据\n",
    "img = cv.imread(\"wu.jpg\")#读入待预测图片\n",
    "img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)#将图片转化为灰度图\n",
    "img_gray = cv.equalizeHist(img_gray)  # 把图像的范围拉开\n",
    "\n",
    "#将训练好的模型，准确率，PCA导入\n",
    "with open(\"clf\", 'rb') as f:\n",
    "   clf = pickle.load(f)#随机森林模型\n",
    "with open(\"acc\", 'rb') as f:\n",
    "   acc = pickle.load(f)#准确率\n",
    "with open(\"pca\",'rb') as f:\n",
    "    pca=pickle.load(f)#PCA\n",
    "\n",
    "# 1. 创建级联分类器\n",
    "face_cascade = cv.CascadeClassifier()\n",
    "# 2. 引入训练好的可用于人脸识别的级联分类器模型\n",
    "face_cascade.load(\"haarcascade_frontalface_alt.xml\")\n",
    "# 3. 用此级联分类器识别图像中的所有人脸信息，返回一个包含有所有识别的人联系系的列表\n",
    "# 列表中每一个元素包含四个值：面部左上角的坐标(x,y) 以及面部的宽和高(w,h)\n",
    "faces = face_cascade.detectMultiScale(img_gray)\n",
    "\n",
    "# 4. 为图像中的所有面部画框\n",
    "for (x, y, w, h) in faces:\n",
    "    cv.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)\n",
    "    img_face1 = cv.resize(img_gray[y:y+h+1, x:x+w+1], (32, 32))#提取面部图像信息\n",
    "    cv.putText(img,  # 要显示字体的图片\n",
    "               str(clf.predict(pca.transform([img_face1.ravel()]))[0]),  # 要显示的内容\n",
    "               (x, y-10),  # 要显示的位置\n",
    "               cv.FONT_HERSHEY_SIMPLEX,  # 要使用的字体 -> 一般英文字体\n",
    "               1,  # 字体放大倍数\n",
    "               (0, 255, 0),  # 字体颜色\n",
    "               2)  # 字体线条粗细\n",
    "cv.putText(img,'acc={:.2f}'.format(acc), (10, 10),cv.FONT_HERSHEY_SIMPLEX,0.4,(255,0,0),1)\n",
    "plt.figure(figsize=(10, 5))\n",
    "plt.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f2bb8e1-e0b3-4179-88f9-da9e740da3a9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
